﻿Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Imports DWGdirect.Colors
Imports DWGdirect.DatabaseServices
Imports DWGdirect.Geometry
Imports DWGdirect.Runtime


Public Class SHPImportFormV2

    '小阳改说明，隐藏按钮，不适用并隐藏隐藏split控件，原来导入主场景按钮变为确定按钮，控制鼠标手势按钮，并未工具栏按钮，显示坐标的label改变



    Protected mRef As LayerManager
    Protected m_manager As ReadOnlyLayerManager

    Protected textList As New List(Of FText)


    Dim entList As New List(Of FEntity)

    Dim fName As String = ""

    Dim lName As String


    Public Sub New(ByRef m As LayerManager, ByVal ln As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。




        If m Is Nothing Then
            MessageBox.Show("空引用传递！")
            Me.Close()

        End If

        mRef = m

        If ln = "" Then
            MessageBox.Show("层ID不能为空！")
            Me.Close()

        End If

        lName = ln







        m_manager = New ReadOnlyLayerManager(AxMap1)

        AddHandler m_manager.AskForRemoveEntity, AddressOf Me.OnAskRemove
        AddHandler m_manager.EntitySelected, AddressOf Me.OnSelected


        '这里是新加的关于显示的坐标
        AddHandler m_manager.mousePosChanged, AddressOf Me.OnMPosChanged

        AxMap1.CursorMode = MapWinGIS.tkCursorMode.cmNone
        AxMap1.MapCursor = MapWinGIS.tkCursor.crsrMapDefault



    End Sub

    Private Sub DXFImportForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load




    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        If txtFileName.Text.Trim() = "" Or Not System.IO.File.Exists(txtFileName.Text.Trim()) Then

            MessageBox.Show("请指定正确的SHP文件", "错误")
            Return
        End If

        





        lblMessage.Text = "正在打开文件请稍后..."


        Me.Cursor = Cursors.WaitCursor

        '清空旧有的逻辑
        entList.Clear()


        m_manager.RemoveLayer("layer")

        fName = txtFileName.Text.Trim()




        'Dim shpFile As New MapWinGIS.Shapefile
        'shpFile.Open(fName)




        Dim shpLayer As New ReadonlyShapeLayer("layer", fName)

        m_manager.LoadLayer(shpLayer, fName)
        





        lblMessage.Text = "完成"




        Me.Cursor = Cursors.Default






    End Sub

   

   



  






    Dim m_layerHanle As Integer = -1



    Protected Sub RemoveLayer()


    End Sub

    Private Sub OnAskRemove(ByVal ln As String, ByVal sn As String)


        Dim cpLayer As EditableShapeLayer
        m_manager.GetLayer(ln, cpLayer)
        If cpLayer IsNot Nothing Then

            cpLayer.RemoveEntity(sn)
        End If


    End Sub

    Private Sub OnSelected(ByVal ln As String, ByVal sn As String)

        ' MessageBox.Show(sn)

    End Sub

    Protected Sub OnMPosChanged(ByVal x As Double, ByVal y As Double)
        '小阳修改
        coodor.Text = "X: " + x.ToString("f3") + "," + "Y:" + y.ToString("f3")
        'lblCoord.Text = "X: " + x.ToString("f3") + "," + "Y:" + y.ToString("f3")

    End Sub

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

     
        OpenFileDialog1.Filter = "ESRI SHP Files (*.shp)|*.shp"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            txtFileName.Text = OpenFileDialog1.FileName
        End If
        If txtFileName.Text.Trim() = "" Or Not System.IO.File.Exists(txtFileName.Text.Trim()) Then

            MessageBox.Show("请指定正确的SHP文件", "错误")
            Return
        End If





        '小阳修改，“导入代码”合并到 “打开文件”中

        lblMessage.Text = "正在打开文件请稍后..."
        Me.Cursor = Cursors.WaitCursor
        '清空旧有的逻辑
        entList.Clear()

        m_manager.RemoveLayer("layer")

        fName = txtFileName.Text.Trim()

        'Dim shpFile As New MapWinGIS.Shapefile
        'shpFile.Open(fName)

        Dim shpLayer As New ReadonlyShapeLayer("layer", fName)

        m_manager.LoadLayer(shpLayer, fName)

        lblMessage.Text = "完成"
        Me.Cursor = Cursors.Default


    End Sub






    Private Sub AxMap1_DblClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxMap1.DblClick
        m_manager.SetToExtent()
    End Sub

    Private Sub AxMap1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles AxMap1.PreviewKeyDown

    End Sub

    Private Sub btnNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNormal.Click
        m_manager.SetMNormal()
        bLocation = False
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        m_manager.SetMSelect()
        bLocation = False
    End Sub

    Private Sub btnHand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHand.Click
        m_manager.SetMHand()
        bLocation = False
    End Sub


    Public Event AskForAddDWGLayer(ByVal entList As List(Of FEntity), ByVal textList As List(Of FText))
    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click

        '计算偏移值
        Dim pNow, pOld, pPre As New MapWinGIS.Point


        pOld.x = Double.Parse(lblX.Text)
        pOld.y = Double.Parse(lblY.Text)

        pNow.x = nudX.Value
        pNow.y = nudY.Value

        pPre.x = pNow.x - pOld.x
        pPre.y = pNow.y - pOld.y

        'If pPre.x = 0 And pPre.y = 0 Then
        'Else

        '    '实体偏移
        '    For Each ent As FEntity In entList

        '        For i As Integer = 0 To ent.ptlist.Length - 1

        '            ent.ptlist(i).x += pPre.x
        '            ent.ptlist(i).y += pPre.y

        '        Next

        '    Next


        '    '文字偏移
        '    For Each text As FText In textList

        '        text.x += pPre.x
        '        text.y += pPre.y

        '    Next


        'End If

        m_manager.RemoveLayer("layer")


        Dim lay As New ReadonlyShapeLayer(lName, fName)


        mRef.AddLayer(lay)




        MessageBox.Show("主场景导入完成！")

        mRef.SetZoomToLayer(lName)
        Me.Close()

        'RaiseEvent AskForAddDWGLayer(entList, textList)

        'Me.Close()




        'Dim layer As ESPolylineLayer
        'm_manager.GetLayer("layer", layer)
        'If layer IsNot Nothing Then

        '    layer.RemoveAllEntity()
        '    layer.AddEntityRange(entList)

        '    layer.Save()

        'Else

        '    MessageBox.Show("请先转换对应的文件")
        '    Return

        'End If














        'Next


    End Sub



    Private Sub GroupBox1_Enter_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

   



    Dim m_iDraw As Integer = 0
    Private Sub DrawText()
        AxMap1.ClearDrawing(m_iDraw)
        m_iDraw = AxMap1.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList)

        AxMap1.set_DrawingLabelsScale(m_iDraw, True)

        For Each dbt As FText In textList

            Dim just As MapWinGIS.tkHJustification = MapWinGIS.tkHJustification.hjRaw

            Select Case dbt.justification

                Case TEXTJUSTIFICATION.CENTER
                    just = MapWinGIS.tkHJustification.hjCenter

                Case TEXTJUSTIFICATION.LEFT
                    just = MapWinGIS.tkHJustification.hjLeft

                Case TEXTJUSTIFICATION.RIGHT
                    just = MapWinGIS.tkHJustification.hjRight


            End Select



            AxMap1.AddDrawingLabel(m_iDraw, dbt.str, 1000, dbt.x, dbt.y, just)


        Next

    End Sub

    Private Sub nudX_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudX.ValueChanged

    End Sub

    Dim bLocation As Boolean = False
    Private Sub btnLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocation.Click

        m_manager.SetAllLayerNoEdditable()

        AxMap1.MapCursor = MapWinGIS.tkCursor.crsrCross

        bLocation = True

    End Sub

    Private Sub SetNotLocation()
        AxMap1.MapCursor = MapWinGIS.tkCursor.crsrMapDefault
        bLocation = False
    End Sub


    Dim m_iDaw2 As Integer = -1
    Protected Sub DrawConcernPoint(ByVal pt As MapWinGIS.Point)
        AxMap1.ClearDrawing(m_iDaw2)
        m_iDaw2 = AxMap1.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList)

        AxMap1.DrawCircleEx(m_iDaw2, pt.x, pt.y, 4, 65025, True)


    End Sub

    Private Sub AxMap1_MouseDownEvent(ByVal sender As System.Object, ByVal e As AxMapWinGIS._DMapEvents_MouseDownEvent) Handles AxMap1.MouseDownEvent
        If bLocation Then
            If e.button = 1 Then

                Dim p As New MapWinGIS.Point
                AxMap1.PixelToProj(e.x, e.y, p.x, p.y)

                Me.DrawConcernPoint(p)

                lblX.Text = p.x.ToString("f2")
                lblY.Text = p.y.ToString("f2")

            ElseIf e.button = 2 Then


                Me.SetNotLocation()

            End If



        End If
    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label10.Click, Label11.Click

    End Sub

    Private Sub lblY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblY.Click

    End Sub

    Private Sub bgwTran_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwTran.DoWork

    End Sub


    Private Sub bgwTran_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwTran.RunWorkerCompleted
        If e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        Else

            If e.Cancelled Then

                MessageBox.Show("取消操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If






            MessageBox.Show("转换操作成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)


        End If





    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        '小阳增加
        m_manager.SetMHand()
        bLocation = False
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        '小阳增加
        m_manager.SetMSelect()
        bLocation = False
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        '小阳增加
        m_manager.SetMNormal()
        bLocation = False
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '小阳加入
        Me.Cursor = Cursors.WaitCursor
        '计算偏移值
        Dim pNow, pOld, pPre As New MapWinGIS.Point


        pOld.x = Double.Parse(lblX.Text)
        pOld.y = Double.Parse(lblY.Text)

        pNow.x = nudX.Value
        pNow.y = nudY.Value

        pPre.x = pNow.x - pOld.x
        pPre.y = pNow.y - pOld.y

        'If pPre.x = 0 And pPre.y = 0 Then
        'Else

        '    '实体偏移
        '    For Each ent As FEntity In entList

        '        For i As Integer = 0 To ent.ptlist.Length - 1

        '            ent.ptlist(i).x += pPre.x
        '            ent.ptlist(i).y += pPre.y

        '        Next

        '    Next


        '    '文字偏移
        '    For Each text As FText In textList

        '        text.x += pPre.x
        '        text.y += pPre.y

        '    Next


        'End If

        m_manager.RemoveLayer("layer")


        If fName = "" Then

            Me.Close()
            Return

        End If

        Dim lay As New ReadonlyShapeLayer(lName, fName)

        mRef.AddLayer(lay)


        '小阳加入
        Me.Cursor = Cursors.Arrow
        MessageBox.Show("主场景导入完成！")

        mRef.SetZoomToLayer(lName)
        Me.Close()

        'RaiseEvent AskForAddDWGLayer(entList, textList)

        'Me.Close()




        'Dim layer As ESPolylineLayer
        'm_manager.GetLayer("layer", layer)
        'If layer IsNot Nothing Then

        '    layer.RemoveAllEntity()
        '    layer.AddEntityRange(entList)

        '    layer.Save()

        'Else

        '    MessageBox.Show("请先转换对应的文件")
        '    Return

        'End If














        'Next

    End Sub
End Class

